package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import javax.validation.constraints.Max;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @author Fengkai 首次日志上传最大积分
 * @creat 2023/4/2
 * 1、每成功上报一条日志，奖励1分
 * 2、每条日志每延迟上报1秒，扣1分
 * 3、积累日志达到100条，必须立即上报
 * 给出日志序列，根据该规则，计算首次上报能获得的最多积分数
 * 输入描述:
 * 按时序产生的日志条数 T1,T2...Tn，其中 1<=n<=1000，0<=Ti<=100
 */
public class GetLogsCount {

    public static void main (String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        String s = scanner.nextLine();
        for (int i = 0; i < 500; i++) {
            int[] ints = new int[((int) (Math.random() * 1000) + 1)];
            for (int j = 0; j < ints.length; j++) {
                ints[j] = (int) (Math.random() * 100) + 1;
            }
            String s = "";
            for (int j = 0; j < ints.length; j++) {
                s += ints[j] + " ";
            }
            String substring = s.substring(0, s.length() - 1);
            if (get(substring) == hisMethod(substring)){
                System.out.println("对比成功");
            }
        }
    }


    public static int get (String input) {
        List<Integer> logList = Arrays.stream(input.split(" ")).map(Integer::parseInt).collect(Collectors.toList());
        if (logList.get(0) >= 100) {
            return 100;
        }
        //动态规划
        int[][] addAndReduce = new int[logList.size()][2];
        addAndReduce[0][0] = logList.get(0);
        addAndReduce[0][1] = 0;
        //记录每个时刻上报的积分
        int[] result = new int[logList.size()];
        result[0] = addAndReduce[0][0] - addAndReduce[0][1];
        //记录每个时刻需要增加的扣分
        int[] reduce = new int[logList.size()];
        reduce[0] = 0;
        //日志条数  超过100必须上报
        int count = logList.get(0);
        for (int i = 1; i < logList.size(); i++) {
            reduce[i] = reduce[i - 1] + logList.get(i - 1);
            count += logList.get(i);
            count = Math.min(count, 100);
            //加分
            addAndReduce[i][0] = count;
            //扣分
            addAndReduce[i][1] = addAndReduce[i - 1][1] + reduce[i];
            //结果
            result[i] = addAndReduce[i][0] - addAndReduce[i][1];
            if (count == 100) {
                break;
            }
        }
        return Arrays.stream(result).max().getAsInt();
    }


    public static int hisMethod (String input) {
        //处理输入
        List<Integer> logs = Arrays.stream(input.split(" "))
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        //加分
        int[] plus_score = new int[logs.size()];
        plus_score[0] = logs.get(0);

        // 减分
        int[] minus_score = new int[logs.size()];
        minus_score[0] = 0;

        int[] result = new int[logs.size()];
        result[0] = logs.get(0);

        for (int i = 1; i < logs.size(); i++) {
            plus_score[i] = Math.min(100, plus_score[i - 1] + logs.get(i));
            minus_score[i] = minus_score[i - 1] + plus_score[i - 1];
            result[i] = plus_score[i] - minus_score[i];

            if (plus_score[i] >= 100) {
                break;
            }
        }

        int max_score = 0; // 最大值
        for (int item : result) {
            if (item > max_score) {
                max_score = item;
            }
        }
        return max_score;
    }

}
